import java.util.*;
class Main
{
	
	int n;
	
	StringBuffer sb, s1, s2;
	String s3;
	int times;
	
	int check(){
		int mark[] = new int[128];
		for (int i=0; i<s1.length(); ++i){
			mark[(int)s1.charAt(i)]++;
			mark[(int)s2.charAt(i)]++;
		}
		for (int i=0; i<s3.length(); ++i){
			mark[(int)s3.charAt(i)]--;
		}
		for (int i=0; i<128; ++i)
			if (mark[i]!=0)
			   return -1;
		return 0;
	}
	
	void shuffle(){
		int i, j, k;
		sb = new StringBuffer(s1.toString()+s2.toString());
		for (k=1; k<=1000; ++k){
			//shuffle
			j = 0;
			for (i=0; i<n; ++i){
				sb.setCharAt(j++, s2.charAt(i));
				sb.setCharAt(j++, s1.charAt(i));
			}	
			
			//System.out.println("shuffer:"+sb.toString());
			
			//compare
			if (sb.toString().compareTo(s3)==0)	{
				times = k;
				return;
			}
			
			// split
			for (i=0; i<n; ++i)
				s1.setCharAt(i, sb.charAt(i));
			
			for (i=n,j=0; i<n*2; ++i)
				s2.setCharAt(j++, sb.charAt(i));
				
		} // end of for
		times = -1;
		
	}
	
	void output(int i){
		System.out.printf("%d %d\n", i, times);
	}
	
	void go()
	{

        Scanner cin = new Scanner(System.in);
        int N, a;
        N = cin.nextInt();
        for (int i=1; i<=N; ++i)
        {
        	n = cin.nextInt();
        	s1 = new StringBuffer(cin.next());
        	s2 = new StringBuffer(cin.next());
        	s3 = cin.next();
        	
        	times = check();
        	if (times==-1) output(i);
        	else
        	{
        		shuffle();	
        		output(i);
        	}
    		
        }
        
	}

    public static void main(String args[]) {
		new Main().go();
    }

}

